package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.ImageSelector.ImageScanner;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.commonclass.SearchActivity;
import com.lishe.lishe.lishe.module.excitation.adapter.SearchCompanyAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.company.SearchCompany;
import com.lishe.lishe.lishe.module.excitation.presenter.ExcitationPresenter;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.retrofit.DataResponseListener;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by yang on 2017/11/20.
 * 搜索企业
 */

public class SearchCompanyActivity extends BaseActivity implements View.OnClickListener {

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, SearchCompanyActivity.class);
        activity.startActivityForResult(intent, JoinCompanyActivity.SEARCH_COMPANY);
    }

    private TextView mTextTitle;
    private Button mButtonBack,
            mButtonShare;
    private EditText mEditComName;
    private RecyclerView mRecyclerList;
    private ImageView mImageIcon, mImageSearchIcon;
    private RelativeLayout mRelativeEmpty;
    private LinearLayout mLinearLoading, mLinearEdit;


    private String mStrComName;
    private SearchCompanyAdapter mAdapter;
    private List<SearchCompany> mList;
    private InputMethodManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_company);
        initView();
        setListener();
        setAdapter();
        if(manager==null){
            manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        }
    }


    private void initView() {
        mEditComName = findViewById(R.id.comName);
        mEditComName.setHorizontallyScrolling(true);
        mImageIcon = findViewById(R.id.icon);
        mButtonBack = findViewById(R.id.title_left_button);
        mTextTitle = findViewById(R.id.title_text_title);
        mRecyclerList = findViewById(R.id.list);
        mButtonShare = findViewById(R.id.share);
        mRelativeEmpty = findViewById(R.id.empty);
        mImageSearchIcon = findViewById(R.id.search_icon);
        mLinearLoading = findViewById(R.id.loading);
        mLinearEdit = findViewById(R.id.ll_edit);

        mTextTitle.setText("搜索我的企业");
        mRecyclerList.setLayoutManager(new LinearLayoutManager(SearchCompanyActivity.this));
        mRecyclerList.addItemDecoration(new SpaceItemDecoration(0, 0, 0, Utils.dip2px(SearchCompanyActivity.this, 0.5f)));
    }

    private void setListener() {
        mButtonBack.setOnClickListener(this);
        mButtonShare.setOnClickListener(this);
        mImageIcon.setOnClickListener(this);
        mLinearEdit.setOnClickListener(this);

        mEditComName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mStrComName = s.toString().trim();
                if (!TextUtils.isEmpty(mStrComName)) {
//                    if (mImageSearchIcon.getVisibility() == View.VISIBLE) {
//                        mImageSearchIcon.setVisibility(View.GONE);
//                    }
                    if (mImageIcon.getVisibility() == View.GONE) {
                        mImageIcon.setVisibility(View.VISIBLE);
                    }
                    mList.clear();
                    mAdapter.notifyDataSetChanged();
                    mLinearLoading.setVisibility(View.VISIBLE);
                    if (mRelativeEmpty.getVisibility() == View.VISIBLE) {
                        mRelativeEmpty.setVisibility(View.GONE);
                    }
                    ExcitationPresenter.getInstance().getComList(Globals.getToken(), mStrComName, listener);
                } else {
//                    if (mImageSearchIcon.getVisibility() == View.GONE) {
//                        mImageSearchIcon.setVisibility(View.VISIBLE);
//                    }
                    if (mImageIcon.getVisibility() == View.VISIBLE) {
                        mImageIcon.setVisibility(View.GONE);
                    }
                    if(mRelativeEmpty.getVisibility()==View.VISIBLE){
                        mRelativeEmpty.setVisibility(View.GONE);
                    }

                    mList.clear();
                    mAdapter.notifyDataSetChanged();
                }

            }
        });

    }

    private void setAdapter() {
        mList = new LinkedList<>();
        mAdapter = new SearchCompanyAdapter(SearchCompanyActivity.this, mList);
        mRecyclerList.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
                back(mList.get(position));
            }
        });
    }


    @Override
    public void onBackPressed() {
        back(null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_button:
                back(null);
                break;
            case R.id.share:
                CallHRJoinActivity.jump(SearchCompanyActivity.this);
                break;
            case R.id.icon:
                mEditComName.setText("");
                break;
            case R.id.ll_edit:
                manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
                break;
        }
    }

    private void back(SearchCompany company) {
        Intent intent = getIntent();
        intent.putExtra("SearchCompany", company);
        if (company == null) {
            setResult(RESULT_CANCELED, intent);
        } else {
            setResult(RESULT_OK, intent);
        }
        finish();
    }

    private void hideInputSoft() {
        manager.hideSoftInputFromWindow(mEditComName.getWindowToken(), 0);
    }

    private DataResponseListener<List<SearchCompany>> listener = new DataResponseListener<List<SearchCompany>>() {

        @Override
        public void onDataEmpty() {
            super.onDataEmpty();
            mLinearLoading.setVisibility(View.GONE);
            mRecyclerList.setVisibility(View.GONE);
            mRelativeEmpty.setVisibility(View.VISIBLE);
        }

        @Override
        public void onSuccess(List<SearchCompany> dataResponse) {
            mLinearLoading.setVisibility(View.GONE);
            if (dataResponse.size() <= 0) {
                mRecyclerList.setVisibility(View.GONE);
                mRelativeEmpty.setVisibility(View.VISIBLE);
            } else {
                mList.clear();
                mList.addAll(dataResponse);
                mAdapter.notifyDataSetChanged();
            }

//            hideInputSoft();
        }

        @Override
        public void onFail(String failMessage) {
            mLinearLoading.setVisibility(View.GONE);
            mRelativeEmpty.setVisibility(View.VISIBLE);
//            toast(failMessage);
        }

        @Override
        public void onError(Throwable e) {
            mLinearLoading.setVisibility(View.GONE);
            toast(getString(R.string.interface_connect_on_error));
            e.printStackTrace();
        }
    };
}
